import {routerFilter} from "../../utils/routerFilter";
import util from '../../utils/util';
import consts from "../../js/const";
import Toast from '@vant/weapp/toast/toast';
routerFilter({

    /**
     * 页面的初始数据
     */
    data: {
        couponUseImageUrl: consts.getImageUrl('coupon_use.png'),
        couponExpireImageUrl: consts.getImageUrl('coupon_expire.png'),
        imageUrl: consts.getImageUrl('coupon.png'),
        params: {
            pageNumber: 1,
            pageSize: 5,
            typeCde: 'COUPON',
            isUse:false,
            isExpire:false
        },
        hasNoUse:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        const eventChannel = that.getOpenerEventChannel();
        eventChannel.on('acceptDataFromOpenerPage', function (data) {
            that.data=util.extend(that.data,data);
            that.setData(that.data, () => {
               that.searchCouponByUserIdAndType(0);
            })
        });

    },
    searchCouponByUserIdAndType(index) {
        let that = this;
        let data = that.data.params;
        let params = util.extend({}, data);
        util.promise('/searchCouponByUserIdAndType', params).then((res) => {
            if (res.data) {
                let couponList = res.data.list || [];
                let hasNoUse=false;
                couponList.forEach(function (item) {
                    hasNoUse=index===0?hasNoUse||!item.isUse:true;
                    if (item.expirationDt) {
                        item.isExpire = util.formatDate(new Date()) > item.expirationDt;
                    }
                });
                that.setData({hasNoUse:hasNoUse,couponList: couponList.concat(data.couponList || [])},function (){
                    if(!hasNoUse && !params.isUse && !params.isExpire){
                        util.promise('/searchEycCouponRuleByTypeCde', params).then((res) => {
                            if(!util.isEmptyObject(res.data)){
                                that.setData({couponList: res.data.concat(data.couponList || [])});
                            }
                        });
                    }
                });


            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    searchCoupons(event) {
        let that = this;
        let params = that.data.params;
        params.isUse = false;
        params.isExpire = false;
        params.pageNumber=1;
        if (event.detail.index === 1) {
            params.isUse = true;
        } else if (event.detail.index === 2) {
            params.isExpire = true;
        }
        that.data.couponList=null;
        that.setData(that.data,()=>{
          that.searchCouponByUserIdAndType(event.detail.index);
        })
    },
    selectCoupon(event){
        let that=this;
        let coupon=event.currentTarget.dataset.coupon;
        const eventChannel = that.getOpenerEventChannel();
        eventChannel.emit('acceptDataFromMyCouponPage', coupon);
        wx.navigateBack({ delta: 1 });
    },
    isShowRemark(event) {
        let that = this;
        let item = event.currentTarget.dataset.item;
        let couponList = event.currentTarget.dataset.couponList;
        couponList.forEach(function (r) {
            if ((r.eycCouponRuleId === item.eycCouponRuleId && !item.eycCouponId) || (item.eycCouponId && item.eycCouponId===r.eycCouponId)) {
                r.isShowRemark = !r.isShowRemark;
            } else {
                r.isShowRemark = false;
            }
        });
        that.setData({couponList: couponList});
    },
    receiveCoupon(event){
        let that = this;
        let item = event.currentTarget.dataset.coupon;
       util.promise('/receiveCoupon',item).then((res)=>{
           if(res.data){
               that.searchCoupons(event);
               Toast(`领取成功`);
           }
       })
    }
})